package com.solution.admin.sysconfig.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.sysconfig.entity.WechatMsgKeywordReplyEntity;
import com.solution.admin.sysconfig.service.WechatMsgKeywordReplyService;
import com.solution.common.constants.PublicStatus;
import com.solution.common.constants.PublicStatus.STATUS;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 微信消息关键字回复web控制层
 * @author solin
 * @date 2017-11-30 14:50:18
 */
@RequestMapping("/wechatMsgKeywordReply")
@Controller
public class WechatMsgKeywordReplyController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(WechatMsgKeywordReplyController.class);

	@Autowired
	private WechatMsgKeywordReplyService wechatMsgKeywordReplyService;
	
	/**
	 * 跳转微信消息关键字回复查询页面
	 * @param WechatMsgKeywordReply 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author solin
 	 * @date 2017-11-30 14:50:18
	 */
	@RequestMapping("/toListWechatMsgKeywordReplyPage")
	public String toListWechatMsgKeywordReplyPage(WechatMsgKeywordReplyEntity wechatMsgKeywordReply, HttpServletRequest request) {
		return "sysconfig/wechatMsgKeywordReply_list";
	}

	/**
	 * 分页查询微信消息关键字回复列表
	 * @param wechatMsgKeywordReply 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author solin
 	 * @date 2017-11-30 14:50:18
	 */
	@RequestMapping("/listWechatMsgKeywordReplyPage")
	@ResponseBody
	public JSONGrid listWechatMsgKeywordReplyPage(WechatMsgKeywordReplyEntity wechatMsgKeywordReply, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("keyword", wechatMsgKeywordReply.getKeyword());
		try {
			PageBean pageBean = wechatMsgKeywordReplyService.listWechatMsgKeywordReplyPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询微信消息关键字回复失败", logger, e);
		}
		return new JSONGrid();
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author solin
 	 * @date 2017-11-30 14:50:18
	 */
	@RequestMapping("/toAddWechatMsgKeywordReply")
	public String toAddWechatMsgKeywordReply(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "sysconfig/wechatMsgKeywordReply_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author solin
 	 * @date 2017-11-30 14:50:18
	 */
	@RequestMapping("/toUpdateWechatMsgKeywordReply")
	public String toUpdateWechatMsgKeywordReply(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			WechatMsgKeywordReplyEntity wechatMsgKeywordReply = wechatMsgKeywordReplyService.getWechatMsgKeywordReplyById(id);
			request.setAttribute("wechatMsgKeywordReply", wechatMsgKeywordReply);
		} catch (Exception e) {
			handleException("跳转编辑微信消息关键字回复页面错误", logger, e);
		}
		return "sysconfig/wechatMsgKeywordReply_form";
	}


	/**
	 * 新增或保存微信消息关键字回复
	 * @param request 请求对象
	 * @param wechatMsgKeywordReplyEntity 微信消息关键字回复实体
	 * @return
	 * @author solin
 	 * @date 2017-11-30 14:50:18
	 */
	@RequestMapping("/saveOrUpdateWechatMsgKeywordReply")
	@ResponseBody
	public Map<String, Object> saveWechatMsgKeywordReply(HttpServletRequest request, WechatMsgKeywordReplyEntity wechatMsgKeywordReplyEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String id = request.getParameter("id");
			if(StringUtil.isNotEmpty(id)){
				wechatMsgKeywordReplyEntity.setModifier(this.getUserCode());
				result = wechatMsgKeywordReplyService.updateWechatMsgKeywordReply(wechatMsgKeywordReplyEntity);
			}else{
				wechatMsgKeywordReplyEntity.setCreater(this.getUserCode());
				wechatMsgKeywordReplyEntity.setStatus(STATUS.ENABLED.getId());
				result = (int)wechatMsgKeywordReplyService.saveWechatMsgKeywordReply(wechatMsgKeywordReplyEntity);
			}
			resultMap.put(PublicStatus.RESULT_CODE, result > 0 ? 1 : 0);
			resultMap.put(PublicStatus.RESULT_MSG, "保存成功");
		} catch (Exception e) {
			resultMap.put(PublicStatus.RESULT_CODE, 0);
			resultMap.put(PublicStatus.RESULT_MSG, "保存失败");
			handleException("添加微信消息关键字回复失败", logger, e);
		}
		return resultMap;
	}
	
}
